home *** CD-ROM | disk | FTP | other *** search
- LISTING 2 - Illustrates Stack Unwinding
-
- // destroy2.cpp
- #include <iostream.h>
-
- class Foo
- {
- public:
- Foo() {cout << "Foo constructor" << endl;}
- ~Foo() {cout << "Foo destructor" << endl;}
- };
-
- void f();
- void g();
-
- main()
- {
- try // Turn on exception handling
- {
- f();
- }
-
-
- catch(...) // Ellipsis matches any exception
- {
- cout << "Caught exception" << endl;
- }
- return 0;
- }
-
- void f()
- {
- Foo x;
-
- g();
- }
-
- void g()
- {
- Foo x;
-
- throw 1;
- }
-
- /* Output:
- Foo constructor
- Foo constructor
- Foo destructor
- Foo destructor
- Caught exception
- */
-